package se.webbzon.boltzmann.minigame.sandbox;

import java.awt.Image;

import se.webbzon.boltzmann.resource.Resources;
import se.webbzon.oschi01.resourceset.ResourceSet;
import se.webbzon.oschi01.worldarea.WorldArea;
import se.webbzon.oschi01.worldcamera.WorldCameraPane;
import se.webbzon.oschi01.worldobject.WorldObject;

public abstract class SandBoxButton extends DefaultSandBoxItem {
	
	// The 2-dimensional depth of this world object.
	private static final int depth = 100;
	
	// The time (nr of steps) the button is hold during press
	private static final int pressTime = 3;
	
	// The available button types
	public enum Type {RIGHT, UP, LEFT, DOWN, OK, NO};
	
	// The image set of this button
	private final ResourceSet<Image> imageSet;
	
	// The remaining time (nr of steps) until the button is released
	private int remaining;
	
	/*============================================================
	Constructors
	============================================================*/
	
	/** **/
	public SandBoxButton(Type type) {
		imageSet = getImageSet(type);
		setInstance(new Instance());
		setSize(32,32);
	}
	
	/*============================================================
	Public Methods
	============================================================*/
	
	/** This method is invoked when this button is pressed. **/
	public abstract void onPress();
	
	/*============================================================
	Private Methods
	============================================================*/
	
	/** Returns the image set associated with the given type. **/
	private static final ResourceSet<Image> getImageSet(Type type) {
		switch (type) {
			case RIGHT:	return Resources.getImageSet("minigame_next_button");
			case UP: 	return Resources.getImageSet("minigame_up_button");
			case LEFT: 	return Resources.getImageSet("minigame_previous_button");
			case DOWN: 	return Resources.getImageSet("minigame_down_button");
			case OK: 	return Resources.getImageSet("minigame_ok_button");
			default: 	return Resources.getImageSet("minigame_close_button");
		}
	}
	
	/*============================================================
	Private Classes
	============================================================*/
	
	private final class Instance extends SandBoxObject implements SandBoxListener {

		public Instance() {
			super(WorldObject.onPostDraw + WorldObject.onStep, depth);
		}

		@Override public void onAttach(SandBox box) {
			box.addListener(this);
		}

		@Override public void onDetach(SandBox box) {
			box.removeListener(this);
		}

		@Override public SandBoxIcon createIcon() {
			return null;
		}

		@Override public void onInvoke(SandBox box, double x, double y) {
			double dx = x - getX() + getWidth()/2, dy = y - getY() + getHeight()/2;
			if (dx >= 0 && dx < getWidth() && dy >= 0 && dy < getHeight()) {
				remaining = pressTime;
				onPress();
			}
		}

		@Override public void onPick(SandBox box, SandBoxItem item, SandBoxContainer container) {};

		@Override public void onDrop(SandBox box, SandBoxItem item, SandBoxContainer container) {};
		
		@Override public void onStep(WorldArea world) {
			if (remaining > 0)
				remaining--;
		}

		@Override public void onPostDraw(WorldArea world, WorldCameraPane hud) {
			int w = (int) getWidth();
			int h = (int) getHeight();
			int x = (int) getX() - w/2;
			int y = (int) getY() - h/2;
			int index = remaining > 0 ? 1 : 0;
			hud.drawImage(imageSet.get(index), x, y, w, h);
		}
		
	}

}
